package com.ms.util.exporter;

import com.ms.domain.viewmodel.FiltersBean;
import com.ms.domain.viewmodel.LabelValueBean;
import com.ms.exception.DpwcBusinessException;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

import java.awt.*;
import java.util.List;
import java.util.ResourceBundle;

/**
 * User: Mlopez
 * Date: 21/03/13
 */
public class PdfExporter {
    private static Font titleFont = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);
    private static Font blueFont = new Font(Font.TIMES_ROMAN, 12, Font.NORMAL, Color.BLUE);

    static ResourceBundle resourceBundle;

    public static void addFilters(Object pdfDocument, FiltersBean filtersBean, ResourceBundle bundle) {
        resourceBundle = bundle;
        try {
            Document document = (Document) pdfDocument;
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document, filtersBean);
            document.add(buildEmptyLines(1));
            document.add(buildText("label_export_report_result", blueFont));
            document.add(buildEmptyLines(1));
        } catch (Exception e) {
            throw new DpwcBusinessException("label_exception_generate_export_report");
        }
    }

    private static void addMetaData(Document document) {
        document.addTitle("label_export_report");
        //    document.addSubject("");
        document.addKeywords("");
        //    document.addAuthor("");
        document.addCreator("label_export_creator");
    }

    private static void addTitlePage(Document document) throws DocumentException {
        document.add(buildEmptyLines(1));
        document.add(buildText("label_export_report_filters", titleFont));
        document.add(buildEmptyLines(1));
    }

    private static void addContent(Document document, FiltersBean filtersBean) throws DocumentException {
        addTable(document, filtersBean.getPrincipalFilters(), "label_export_report_principal_filters");
        if (!filtersBean.getAdvancedFilters().isEmpty()) {
            addTable(document, filtersBean.getAdvancedFilters(), "label_export_report_advanced_filters");
        }
    }

    private static Paragraph buildText(String text, Font font) {
        Paragraph preface = new Paragraph();
        preface.add(new Paragraph(resourceBundle.getString(text), font));
        return preface;
    }

//    protected static ResourceBundle getResourceBundle() {
//        return ExporterUtil.getResourceBundle();
//    }

    private static void addTable(Document document, List<LabelValueBean> labelValueFilters, String title) throws DocumentException {
        document.add(buildText(title, blueFont));
        document.add(buildEmptyLines(1));
        document.add(createTable(labelValueFilters));
    }

    private static PdfPTable createTable(List<LabelValueBean> labelValueFilters) throws DocumentException {
        PdfPTable table = new PdfPTable(2);
        table.setHeaderRows(1);

        PdfPCell labelCell = new PdfPCell(new Phrase(resourceBundle.getString("label_export_report_label_filter_cell")));
        labelCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(labelCell);

        PdfPCell valueCell = new PdfPCell(new Phrase(resourceBundle.getString("label_export_report_value_filter_cell")));
        valueCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(valueCell);

        for (LabelValueBean labelValue : labelValueFilters) {
            table.addCell(resourceBundle.getString(labelValue.getLabel()));
            table.addCell(labelValue.getValue());
        }
        return table;
    }

    private static Paragraph buildEmptyLines(int number) {
        Paragraph paragraph = new Paragraph(" ");
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
        return paragraph;
    }

}
